package com.sanpu.kxe.business.member.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sanpu.kxe.R;
import com.sanpu.kxe.business.member.bean.MemberLevelOutput;
import com.sanpu.kxe.business.member.interfaces.IMemberLevelListener;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;

import java.util.ArrayList;

/**
 * Description：会员等级列表
 * Created by 薛勇军 on 2018/7/30.
 */

public class MemberLevelsListAdapter extends BaseQuickAdapter<MemberLevelOutput, BaseViewHolder> {

    private int mCurrentLvl;

    private IMemberLevelListener levelListener;

    public MemberLevelsListAdapter() {
        super(R.layout.item_member_levels, new ArrayList<MemberLevelOutput>());
    }

    /**
     * 当前等级与升级等级
     */
    public void setData(int mCurrentLvl, IMemberLevelListener levelListener) {
        this.mCurrentLvl = mCurrentLvl;
        this.levelListener = levelListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, final MemberLevelOutput item) {
        ImageView levelImagePathIv = helper.getView(R.id.levelImagePathIv);
        TextView levelNameTv = helper.getView(R.id.levelNameTv);
        TextView descriptionTv = helper.getView(R.id.descriptionTv);
        TextView amountTv = helper.getView(R.id.amountTv);
        LinearLayout agreementLl = helper.getView(R.id.agreementLl);
        ImageView chooseIv = helper.getView(R.id.chooseIv);
        TextView chooseTv = helper.getView(R.id.chooseTv);
        TextView agreementTv = helper.getView(R.id.agreementTv);
        TextView gradeTv = helper.getView(R.id.gradeTv);
        TextView currentTv = helper.getView(R.id.currentTv);

        if (item != null) {
            JKImageLoader.getInstance().displayImage(
                    mContext, UrlConstants.getBaseUrl() + item.getLevelImagePath(), levelImagePathIv);
            if (!TextUtils.isEmpty(item.getLevelName())) {
                levelNameTv.setText(item.getLevelName());
            }
            if (mCurrentLvl == item.getLevelId()) {
                //当前
                currentTv.setVisibility(View.VISIBLE);
                gradeTv.setVisibility(View.GONE);
                agreementLl.setVisibility(View.GONE);
            } else if (mCurrentLvl > item.getLevelId()) {
                //低于当前
                agreementLl.setVisibility(View.GONE);
                currentTv.setVisibility(View.GONE);
                gradeTv.setVisibility(View.GONE);
            } else {
                //高于当前
                currentTv.setVisibility(View.GONE);
                if (item.isAgreement()) {
                    agreementLl.setVisibility(View.VISIBLE);
                    if (item.isChoose()) {
                        chooseIv.setImageResource(R.drawable.ic_choose_checked);
                    } else {
                        chooseIv.setImageResource(R.drawable.ic_choose_noraml);
                    }
                } else {
                    agreementLl.setVisibility(View.GONE);
                }
                if (TextUtils.equals("RECHARGE", item.getUpgradeWay())) {
                    gradeTv.setVisibility(View.VISIBLE);
                } else {
                    gradeTv.setVisibility(View.GONE);
                }
            }
            if (TextUtils.equals("RECHARGE", item.getUpgradeWay())) {
                descriptionTv.setVisibility(View.GONE);
                amountTv.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(item.getRechargeAmount())) {
                    amountTv.setText("￥" + item.getRechargeAmount() + "");
                }
            } else {
                descriptionTv.setVisibility(View.VISIBLE);
                amountTv.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(item.getDescription())) {
                    descriptionTv.setText(item.getDescription());
                }
            }
            final int curPosition = helper.getLayoutPosition();
            if (levelListener != null) {
                chooseIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        levelListener.onChangeAgreementClick(curPosition, item.isChoose());
                    }
                });
                chooseTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        levelListener.onChangeAgreementClick(curPosition, item.isChoose());
                    }
                });
                agreementTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        levelListener.onAgreementClick();
                    }
                });
                gradeTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        levelListener.onGradeMemberClick(item);
                    }
                });
            }
        }
    }
}
